import React, { Component } from 'react'
import { connect } from 'dva';
import {NavLink, Switch, Route} from 'dva/router';
import {
    Button,
    Form,
    Input,
    Select,
    Row,
    Col,
    Radio,
    message
} from 'antd';
import styles from './index.less';

// import InsurCompany from '../../pages/ProductManage/InsurCompany/InsurCompany';
// import NewInsurCompany from '../../pages/ProductManage/InsurCompany/NewInsurCompany';

const { Option } = Select;

@connect(({ add_menu, loading }) => ({
    add_menu,
    loading: loading.models.add_menu,
}))

class MenuPopup extends Component {
    state = {
        visible: false,
        value: 0, // 0：上架，1：下架
        selectedRowKeys: [],
        selectedRows: 0,
        selectedRowData: {},  //-- 选中行数据
    };

    componentDidMount() {
        const parent = this.props.parent;
        this.setState({ selectedRows: parent.state.selectedRows })
    }

    /**
     * 新增
     */
    addData = () => {
        const parent = this.props.parent;
        const visible = parent.state.visible;
      const { selectedRows } = this.props.parent.state;
      if (selectedRows === 0) {
        parent.setState({
          visible: true
        });
        console.log(parent.state.modalKey);
      } else {
        message.info('当前为数据编辑状态，请先取消勾选！');
      }
    };

    /**
     * 编辑
     */
    editInfo = () => {
        const parent = this.props.parent;
        const visible = parent.state.visible;
        // 如果选择表格的行数等于1，就显示“编辑菜单”弹框，否则提示“请选择一条数据”
        const { selectedRows, selectedRowData } = this.props.parent.state;
        if (selectedRows === 1) {
            parent.setState({
                visible: true,
                selectedRowData: selectedRowData
            });
            console.log(selectedRowData);
            //this.getFieldsValue = this.props.form.getFieldsValue;//获得表单所有控件的值
            // console.log(this.props.form.getFieldsValue);
            // console.log(this.props.form.getFieldsValue("code"))
        } else {
            message.info('请选择一条数据');
        }
    };

    /**
     * 上架
     */
    upperShelf = () => {
        // 如果选择表格的行数大于0，上架，否则提示“请选择一条数据”
        if (this.props.parent.state.selectedRows === 1) {
            const parent = this.props.parent;
            const { dispatch } = this.props;
            const _url = this.props.parent.state.actionUrl;
            const thisCtx = this;
            const { selectedRowData } = this.props.parent.state;
            console.log(selectedRowData);
            const isenabled = selectedRowData.isenabled;
            const id = selectedRowData.id;
            let params = {};
            params.isenabled = 1;
            params.id = id;
            console.log(params);
            dispatch({
                type: _url,
                payload: params,
                callback: function (data) {
                    if (data.success) {
                        parent.getTableData();
                        message.success('保存成功')
                    } else {
                        message.error(data.msg);
                        message.error('保存失败')
                    }
                }
            });
        } else {
            message.info('请选择一条数据')
        }
    };

    /**
     * 下架
     */
    lowerShelf = () => {
        // 如果选择表格的行数大于0，下架，否则提示“请选择一条数据”
        if (this.props.parent.state.selectedRows == 1) {
            const parent = this.props.parent;
            const { dispatch } = this.props;
            const _url = this.props.parent.state.actionUrl;
            const thisCtx = this;
            const { selectedRowData } = this.props.parent.state;
            const isenabled = selectedRowData.isenabled;
            const id = selectedRowData.id;
            let params = {};
            params.isenabled = 0;
            params.id = id;
            dispatch({
                type: _url,
                payload: params,
                callback: function (data) {
                    if (data.success) {
                        parent.getTableData(); // 重新调用主页面的菜单列表接口
                        message.success('保存成功')
                    } else {
                        message.error(data.msg);
                        message.success('保存失败')
                    }
                }
            });
        } else {
            message.info('请选择一条数据')
        }
    };

    /**
     * 删除
     */
    delData = () => {
        // 如果选择表格的行数大于0，下架，否则提示“请选择一条数据”
        if (this.props.parent.state.selectedRows == 1) {
            const parent = this.props.parent;
            const { dispatch } = this.props;
            const _url = this.props.parent.state.actionUrl;
            const thisCtx = this;
            const { selectedRowData } = this.props.parent.state;
            const isenabled = selectedRowData.isenabled;
            const id = selectedRowData.id;
            let params = {};
            params.isenabled = -1;
            params.id = id;
            dispatch({
                type: _url,
                payload: params,
                callback: function (data) {
                    if (data.success) {
                        console.log(data);
                        parent.getTableData(); // 重新调用主页面的菜单列表接口
                        // parent.state.setState({selectedRowData: '{}'})
                        // parent.state.setState({selectedRows: 0})
                        console.log(parent.state.selectedRowData);
                        message.success('删除成功')
                    } else {
                        message.error(data.msg);
                    }
                }
            });
        } else {
            message.info('请选择一条数据')
        }
    };

    render() {
        const { getFieldDecorator } = this.props.form;
        const { visible, selectedRowData } = this.state;
        const selectedRows = this.state.selectedRows; // 获取从父组件中传过来的已选择的行数
        const hasSelected = selectedRows > 0; // 是否勾选一条数据

        return (
            <div>
                <div className={styles.btnGroup}>
                    <Button type="primary" onClick={() => this.addData()}>新增</Button>
                    <Button type="primary" onClick={() => this.editInfo()}>编辑</Button>
                    <Button type="primary" onClick={() => this.upperShelf()}>上架</Button>
                    <Button type="primary" onClick={() => this.lowerShelf()}>下架</Button>
                    <Button type="primary" onClick={() => this.delData()}>删除</Button>
                    {/* <div style={{display: 'inline-block'}}>
                        <NavLink to='/productManage/insurCompany' style={{marginRight: '10px'}}>保险公司</NavLink>
                        <NavLink to='/appliForPractice/practice' style={{marginRight: '10px'}}>执业证申请</NavLink>
                        <NavLink to='/productManage/sortOfInsurType' style={{marginRight: '10px'}}>险种分类</NavLink>
                        <NavLink to='/productManage/configure' style={{marginRight: '10px'}}>配置</NavLink>
                        <NavLink to='/memberCenter' style={{marginRight: '10px'}}>会员中心</NavLink>
                    </div> */}

                    <span style={{ marginLeft: 8 }}>
                        {hasSelected ? `已选中 ${selectedRows} 项` : ''}
                    </span>
                </div>
            </div>
        )
    }
}

export default MenuPopup = Form.create()(MenuPopup);
